package org.i_plus;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class CheckFilter
 */
public class CheckFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub
	}
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		//chain.doFilter(request, response);
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse resp = (HttpServletResponse)response;
		
		HttpSession session = req.getSession();
		String account = (String)session.getAttribute("account");
		if(account!=null){
			chain.doFilter(req, resp);
		}else{
			String uri = req.getRequestURI();
			session.setAttribute("destination", uri);
			
			String path = req.getContextPath();
			resp.sendRedirect(path+"/secure/login.jsp");
		}
	}
    private FilterConfig config;
	public void init(FilterConfig config) throws ServletException {
		this.config = config;
	}

}
